﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class OrderDetail
    {
        public int Add(Chain.Model.OrderDetail model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into OrderDetail(");
            builder.Append("OrderID,GoodsID,OrderDetailPrice,OrderDetailPoint,OrderDetailDiscountPrice,OrderDetailNumber,OrderDetailType)");
            builder.Append(" values (");
            builder.Append("@OrderID,@GoodsID,@OrderDetailPrice,@OrderDetailPoint,@OrderDetailDiscountPrice,@OrderDetailNumber,@OrderDetailType)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderID", SqlDbType.Int, 4), new SqlParameter("@GoodsID", SqlDbType.Int, 4), new SqlParameter("@OrderDetailPrice", SqlDbType.Money, 8), new SqlParameter("@OrderDetailPoint", SqlDbType.Int, 4), new SqlParameter("@OrderDetailDiscountPrice", SqlDbType.Money, 8), new SqlParameter("@OrderDetailNumber", SqlDbType.Float, 8), new SqlParameter("@OrderDetailType", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.OrderID;
            cmdParms[1].Value = model.GoodsID;
            cmdParms[2].Value = model.OrderDetailPrice;
            cmdParms[3].Value = model.OrderDetailPoint;
            cmdParms[4].Value = model.OrderDetailDiscountPrice;
            cmdParms[5].Value = model.OrderDetailNumber;
            cmdParms[6].Value = model.OrderDetailType;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int OrderDetailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrderDetail ");
            builder.Append(" where OrderDetailID=@OrderDetailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = OrderDetailID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public int DeleteDetail(int inOrderID)
        {
            return DbHelperSQL.ExecuteSql((" delete from OrderDetail where OrderID=" + inOrderID).ToString());
        }

        public int DeleteItem(int intOrder, int intGoodsID)
        {
            return DbHelperSQL.ExecuteSql(string.Concat(new object[] { " delete from OrderDetail where OrderID=", intOrder, " and GoodsID=", intGoodsID }));
        }

        public bool DeleteList(string OrderDetailIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrderDetail ");
            builder.Append(" where OrderDetailID in (" + OrderDetailIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int OrderDetailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from OrderDetail");
            builder.Append(" where OrderDetailID=@OrderDetailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = OrderDetailID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetDetail(int intOrderID)
        {
            string sQLString = " select * from V_GoodsReturn where OrderID=" + intOrderID;
            string str2 = string.Format("SELECT SUM(OrderDetailNumber) AS AllNumber FROM V_GoodsReturn WHERE OrderID ={0}", intOrderID);
            DataSet set = new DataSet();
            set.Tables.Add(DbHelperSQL.Query(sQLString).Tables[0].Copy());
            DataTable table = DbHelperSQL.Query(str2).Tables[0];
            table.TableName = "AllNumber";
            set.Tables.Add(table.Copy());
            return set;
        }

        public DataSet GetExpenseDetail(int PageSiza, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "OrderDetail,OrderLog,Mem,Goods";
            string[] column = new string[] { "ABS(OrderDetail.OrderDetailNumber) as OrderDetailNumber,OrderDetail.GoodsID,OrderDetail.OrderDetailDiscountPrice,OrderDetail.OrderDetailType,OrderLog.OrderAccount,OrderLog.OrderCreateTime,Mem.MemName,Mem.MemCard " };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "OrderLog.OrderCreateTime", "OrderDetail.OrderID", false, PageSiza, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetGoodsExpenseDetail(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select ABS(OrderDetail.OrderDetailNumber) as OrderDetailNumber,OrderDetail.GoodsID,OrderDetail.OrderDetailDiscountPrice,OrderDetail.OrderDetailType, ");
            builder.Append(" OrderLog.OrderAccount,OrderLog.OrderCreateTime,Mem.MemName,Mem.MemCard from OrderDetail,OrderLog,Mem");
            builder.Append(" where OrderDetail.OrderID = OrderLog.OrderID and OrderLog.OrderMemID=Mem.MemID ");
            if (strWhere != "")
            {
                builder.Append(strWhere);
            }
            builder.Append(" order by OrderLog.OrderCreateTime desc ");
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select OrderDetailID,OrderID,GoodsID,OrderDetailPrice,OrderDetailPoint,OrderDetailDiscountPrice,OrderDetailNumber,OrderDetailType ");
            builder.Append(" FROM OrderDetail ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" OrderDetailID,OrderID,GoodsID,OrderDetailPrice,OrderDetailPoint,OrderDetailDiscountPrice,OrderDetailNumber,OrderDetailType ");
            builder.Append(" FROM OrderDetail ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.OrderDetailID desc");
            }
            builder.Append(")AS Row, T.*  from OrderDetail T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select OrderDetail.*,Goods.Name,Goods.GoodsCode,Goods.GoodsType");
            builder.Append(" from OrderDetail,Goods,OrderLog");
            builder.Append(" where " + strWhere);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("OrderDetailID", "OrderDetail");
        }

        public Chain.Model.OrderDetail GetModel(int OrderDetailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 OrderDetailID,OrderID,GoodsID,OrderDetailPrice,OrderDetailPoint,OrderDetailDiscountPrice,OrderDetailNumber,OrderDetailType from OrderDetail ");
            builder.Append(" where OrderDetailID=@OrderDetailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = OrderDetailID;
            Chain.Model.OrderDetail detail = new Chain.Model.OrderDetail();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["OrderDetailID"] != null) && (set.Tables[0].Rows[0]["OrderDetailID"].ToString() != ""))
            {
                detail.OrderDetailID = int.Parse(set.Tables[0].Rows[0]["OrderDetailID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderID"] != null) && (set.Tables[0].Rows[0]["OrderID"].ToString() != ""))
            {
                detail.OrderID = int.Parse(set.Tables[0].Rows[0]["OrderID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GoodsID"] != null) && (set.Tables[0].Rows[0]["GoodsID"].ToString() != ""))
            {
                detail.GoodsID = int.Parse(set.Tables[0].Rows[0]["GoodsID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderDetailPrice"] != null) && (set.Tables[0].Rows[0]["OrderDetailPrice"].ToString() != ""))
            {
                detail.OrderDetailPrice = decimal.Parse(set.Tables[0].Rows[0]["OrderDetailPrice"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderDetailPoint"] != null) && (set.Tables[0].Rows[0]["OrderDetailPoint"].ToString() != ""))
            {
                detail.OrderDetailPoint = int.Parse(set.Tables[0].Rows[0]["OrderDetailPoint"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderDetailDiscountPrice"] != null) && (set.Tables[0].Rows[0]["OrderDetailDiscountPrice"].ToString() != ""))
            {
                detail.OrderDetailDiscountPrice = decimal.Parse(set.Tables[0].Rows[0]["OrderDetailDiscountPrice"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderDetailNumber"] != null) && (set.Tables[0].Rows[0]["OrderDetailNumber"].ToString() != ""))
            {
                detail.OrderDetailNumber = decimal.Parse(set.Tables[0].Rows[0]["OrderDetailNumber"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderDetailType"] != null) && (set.Tables[0].Rows[0]["OrderDetailType"].ToString() != ""))
            {
                detail.OrderDetailType = int.Parse(set.Tables[0].Rows[0]["OrderDetailType"].ToString());
            }
            return detail;
        }

        public DataSet GetOrderDetail(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select OrderDetail.*,Goods.Name,Goods.GoodsCode,OrderLog.OrderRemark ");
            builder.Append(" from OrderDetail,Goods,OrderLog");
            builder.Append(" where OrderDetail.GoodsID=Goods.GoodsID AND OrderLog.OrderID = OrderDetail.OrderID and " + strWhere);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM OrderDetail ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public string ProjectName(int OrderMemID, int OrderID)
        {
            string str = string.Empty;
            StringBuilder builder = new StringBuilder();
            builder.Append("select ProjectName from ");
            builder.Append("ordertime,OrderLog,TimingProject ");
            builder.Append("where ordertime.OrderTimeCode=OrderLog.OrderAccount ");
            builder.Append("and OrderLog.OrderMemID=@OrderMemID and OrderType=1 and OrderID=@OrderID ");
            builder.Append("and TimingProject.ProjectID=ordertime.OrderProjectID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderMemID", SqlDbType.Int, 4), new SqlParameter("@OrderID", SqlDbType.Int, 4) };
            cmdParms[0].Value = OrderMemID;
            cmdParms[1].Value = OrderID;
            DataTable table = DbHelperSQL.Query(builder.ToString(), cmdParms).Tables[0];
            if (table.Rows.Count > 0)
            {
                str = table.Rows[0][0].ToString();
            }
            return str;
        }

        public int Update(Chain.Model.OrderDetail model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update OrderDetail set ");
            builder.Append("OrderID=@OrderID,");
            builder.Append("GoodsID=@GoodsID,");
            builder.Append("OrderDetailPrice=@OrderDetailPrice,");
            builder.Append("OrderDetailPoint=@OrderDetailPoint,");
            builder.Append("OrderDetailDiscountPrice=@OrderDetailDiscountPrice,");
            builder.Append("OrderDetailNumber=@OrderDetailNumber,");
            builder.Append("OrderDetailType=@OrderDetailType");
            builder.Append(" where OrderDetailID=@OrderDetailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderID", SqlDbType.Int, 4), new SqlParameter("@GoodsID", SqlDbType.Int, 4), new SqlParameter("@OrderDetailPrice", SqlDbType.Money, 8), new SqlParameter("@OrderDetailPoint", SqlDbType.Int, 4), new SqlParameter("@OrderDetailDiscountPrice", SqlDbType.Money, 8), new SqlParameter("@OrderDetailNumber", SqlDbType.Float, 8), new SqlParameter("@OrderDetailType", SqlDbType.Int, 4), new SqlParameter("@OrderDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.OrderID;
            cmdParms[1].Value = model.GoodsID;
            cmdParms[2].Value = model.OrderDetailPrice;
            cmdParms[3].Value = model.OrderDetailPoint;
            cmdParms[4].Value = model.OrderDetailDiscountPrice;
            cmdParms[5].Value = model.OrderDetailNumber;
            cmdParms[6].Value = model.OrderDetailType;
            cmdParms[7].Value = model.OrderDetailID;
            int num = DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
            if (num > 0)
            {
                return num;
            }
            return 0;
        }
    }
}

